

var advList=[];  //广告数据
var activity;  //活动
var status;		//未开始、进行中、已结束

var sh;  //计时方法
apiready = function(){
    
    activity=api.pageParam.activity;
    
    $api.text($api.byId('activitytitle'), activity.name);
    
//  getData();

	advList = activity.imglist;
	openScrollPicture();
     
    fresh(); 
	sh=setInterval(fresh,1000);  //一秒钟计一次
   
    openframe(); //打开抢购数据frame
};


//获取首页信息
function getData(){

   $api.text($api.byId('activitytitle'), activity.name);
   
   do_ajax('/app/getcommAdvert','get',{},function(ret,err){
       
       if(ret){
           advList =ret.ad1;
	       openScrollPicture();
	       
	       api.hideProgress();
       }else{
            api.alert({
	            msg:('错误码：'+err.code+'；错误信息：'+err.msg+'网络状态码：'+err.statusCode)
	        });
	        api.hideProgress();
       
       }
   
   
   });
   


};


//设置滑动图片
function openScrollPicture(){

    var paths = [];
    
   
    for(var i=0;i<advList.length;i++){
	     paths.push(serviceUrl+advList[i].url);
	}
    
   // alert(JSON.stringify(paths));
    
	var obj = api.require('UIScrollPicture');
	
//	var $right = $api.byId('rightw');
//	var rightwidth=$api.offset($right).w;   //右边宽度
//	var picturewidth=api.winWidth-rightwidth;  
	
	
	obj.open({
	    rect: {
	        x: 0, y:50, w:api.winWidth, h:200
	    },
	    data: {
	        paths: paths,
	    },
	    styles: {
	        indicator: {
	            align: 'right',
	            color: '#798183',
	            activeColor: '#fc5101'
	        }
	    },
	    placeholderImg: 'widget://res/slide1.jpg',
	    contentMode: 'scaleToFill',
	    interval: 3,
	    loop: true,
	    fixed: false
	}, function(ret, err){
	     if(ret.status){
	        if(ret.eventType == 'click'){
	            //点击图片的操作
	            //alert(ret.index);
	        }
	     }
	});

};


//打开框架页面
function  openframe(){

  //alert(JSON.stringify(articlelist));
  
   api.openFrame({
        name: 'grabbuy_frm',
        url:'grabbuy_frm.html',
        opaque: true,
        reload:true,
        vScrollBarEnabled: false,
 
        pageParam:{activityName:activity.name,activityId:activity._id,status:status},  //活动名称
      
        rect: {
            x: 0,
            y: 300,
            w: 'auto',
            h: api.winHeight-300,
        }
    });

};

var c=0;
//倒计时
function fresh(){
    
    var startdate=activity.startdate.replace(/-/g, "/");     //活动开始日期
    var enddate=activity.enddate.replace(/-/g, "/");     //活动结束日期
    
    var starttime=activity.starttime+':00';     //活动开始时间
    var endtime=activity.endtime+':00';     //活动结束时间
    
    var startdateStr = startdate + ' ' + starttime;
    var enddateStr = enddate + ' ' + endtime;
    
  //  var starttime=new Date("2015-11-04,15:50:00");   //开始时间

	var starttime=new Date(startdateStr);   //开始时间
    var endtime=new Date(enddateStr);   //结束时间
    
    var nowtime = new Date();    //现在时间
    
    var nostarttime=parseInt((starttime.getTime()-nowtime.getTime())/1000);   //未开始时间
    var leftsecond=parseInt((endtime.getTime()-nowtime.getTime())/1000);   //已开始时间
    
    
//  alert(nostarttime);
//  alert(leftsecond);
    if(nostarttime>0){
        
        d1=parseInt(nostarttime/3600/24);
	    h1=parseInt((nostarttime/3600)%24)+d1*24;
	    m1=parseInt((nostarttime/60)%60);
	    s1=parseInt(nostarttime%60);
	    
       //未开始
       $api.text($api.byId('statustitle'), '距离本场开始：');
       $api.html($api.byId('times'), h1+":"+m1+":"+s1);
       status = '未开始';
	   
       //发送一次
       if(c==0){
	       //发送活动状态改变事件 通知活动未开始
		   api.sendEvent({
			    name: 'changeactivitystatus',
			    extra: '未开始'                              
		   });
		   c=1;
	   }
	   
    }else{
        //进行中
	    d=parseInt(leftsecond/3600/24);
	    h=parseInt((leftsecond/3600)%24)+d*24;
	    m=parseInt((leftsecond/60)%60);
	    s=parseInt(leftsecond%60);
	   
	    $api.html($api.byId('times'), h+":"+m+":"+s);
	    status = '进行中';
	    
	    if(leftsecond<=0){
	        $api.html($api.byId('times'), '本场抢购已结束');
	        $api.css($api.byId('statustitle'),'display:none');
		    clearInterval(sh);
		    status = '已结束';
		    
		     //发送活动状态改变事件 通知活动结束
			api.sendEvent({
			    name: 'changeactivitystatus',
			    extra: '已结束'                              
			});
	    }
    }
};







